package org.example.TankGan.pojo.Enemy;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.example.TankGan.pojo.Bullet.Bullet;
import org.example.TankGan.pojo.Tank;
import org.example.Utils.ShootBulletUtlis;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Vector;

/**
 * @author cc
 * @date 2021/4/17 21:01
 * @Description
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class EnemyTank extends Tank implements Runnable {
    private boolean isLive=true;
    Vector<Bullet> bullets = new Vector<Bullet>();
    private int BulletsNums=1;
    private int BulletSpeed ;
     private static Vector<EnemyTank> enemyTanks = new Vector<>();//为啥用static因为staic会在堆区生成唯一的一个对象（让其不属于属性），不用static的话，将会无限生成这个属性。导致内存溢出

    public EnemyTank(int x, int y, int drection, int speed) {
        super(x, y, drection, speed);
    }

    public EnemyTank(int x, int y, int drection, int speed, int type) {
        super(x, y, drection, speed, type);
    }

    public EnemyTank(int x, int y, int drection, int speed, int type,int EnemyBulletspeed) {
        super(x, y, drection, speed, type);
        this.BulletSpeed=EnemyBulletspeed;
    }


    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }

    @Override
    public void run() {
        MoveAndLimiteAramAndShort();
    }

    private void MoveAndLimiteAramAndShort(){

        while (true){

            if (isLive&&bullets.size()==0){
                Bullet bullet =null;
                int tempSpeed = BulletSpeed+getSpeed();
//                switch (getDrection()){
//                    case 0:
//                        bullet = new Bullet(getX()+20,getY(),0,getSpeed()+40);
//                        break;
//                    case 1:
//                        bullet=new Bullet(getX()+60,getY()+20,1,getSpeed()+40);
//                        break;
//                    case 2:
//                        bullet=new Bullet(getX()+20,getY()+60,2,getSpeed()+40);
//                        break;
//                    case 3:
//                        bullet=new Bullet(getX(),getY()+20,3,getSpeed()+40);
//                        break;
//                }
                //调用创建子弹类
                Vector<Bullet> shut = ShootBulletUtlis.Shut(getX(), getY(), getDrection(), tempSpeed, BulletsNums);
                for (int i = 0; i < shut.size(); i++) {
                    bullet = shut.get(i);
                    new Thread(bullet).start();
                    bullets.add(bullet);
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            switch (getDrection()){
                case 0:
                    for (int i = 0; i < 30; i++) {
                        if(getY()>=12&&!isToochTank()){
                            Upmove();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1:
                    for (int i = 0; i < 30; i++) {
                        if (getX()+60<980&&!isToochTank()){
                            rightMove();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < 30; i++) {
                        if (getY()+60<700&&!isToochTank()){
                            downmove();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < 30; i++) {
                        if (getX()>0&&!isToochTank()){
                            leftMpve();

                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }
            //随机改变坦克方向
            setDrection((int)(Math.random()*4));
            //结束线程
            if (!isLive){
                break;
            }
        }
    }

    public boolean isToochTank(){
        switch (this.getDrection()){
            case 0:
                //取出所有坦克
                for (int i = 0; i < enemyTanks.size(); i++) {
                    //取出一辆坦克
                    EnemyTank enemyTank_temp = enemyTanks.get(i);
                    //先看坦克是否还存活
                    if (enemyTank_temp.isLive){
                        //判断比较的坦克是不是自己
                        if (enemyTank_temp!=this){
                            //判断敌人坦克是朝上还朝下
                            if (enemyTank_temp.getDrection()==0||enemyTank_temp.getDrection()==2){
                                //1.判断敌人x的范围 2.判断敌人y的范围
                                //判断当前坦克左上角坐标
                                if (this.getX()>=enemyTank_temp.getX()
                                        &&this.getX()<=enemyTank_temp.getX()+40
                                        &&this.getY()>=enemyTank_temp.getY()
                                        &&this.getY()<=enemyTank_temp.getY()+60){
                                    return true;//说明碰撞了
                                }
                                //获取当前坦克右上角坐标
                                if (this.getX()+40>=enemyTank_temp.getX()
                                        &&this.getX()+40<=enemyTank_temp.getX()+40
                                        &&this.getY()>=enemyTank_temp.getY()
                                        &&this.getY()<=enemyTank_temp.getY()+60){
                                    return true;//说明碰撞了
                                }
                                //敌人左和右的判断
                            }else if (enemyTank_temp.getDrection()==1||enemyTank_temp.getDrection()==3){
                                //判断当前坦克左上角坐标
                                if (this.getX()>=enemyTank_temp.getX()
                                        &&this.getX()<=enemyTank_temp.getX()+60
                                        &&this. getY()>=enemyTank_temp.getY()
                                        &&this.getY()<=enemyTank_temp.getY()+40){
                                    return true;//说明碰撞了
                                }
                                //判断当前坦克右上角坐标
                                if (this.getX()+40>=enemyTank_temp.getX()
                                        &&this.getX()+40<=enemyTank_temp.getX()+60
                                        &&this. getY()>=enemyTank_temp.getY()
                                        &&this.getY()<=enemyTank_temp.getY()+40){
                                    return true;//说明碰撞了
                                }
                            }
                        }
                    }
                }
                break;
            case 1:
                for (int i = 0; i < enemyTanks.size(); i++) {
                    //取出一辆坦克
                    EnemyTank enemyTank_temp = enemyTanks.get(i);
                    //先看坦克是否还存活
                    if (enemyTank_temp.isLive){
                        //判断比较的坦克是不是自己
                        if (enemyTank_temp!=this){
                            //判断敌人坦克是朝上还朝下
                            if (enemyTank_temp.getDrection()==0||enemyTank_temp.getDrection()==2){
                                //1.判断敌人x的范围 2.判断敌人y的范围
                                //判断当前坦克右上角坐标
                                if (this.getX()+60>=enemyTank_temp.getX()
                                        &&this.getX()+60<=enemyTank_temp.getX()+40
                                        &&this.getY()>=enemyTank_temp.getY()
                                        &&this.getY()<=enemyTank_temp.getY()+60){
                                    return true;//说明碰撞了
                                }
                                //获取当前坦克右下角坐标
                                if (this.getX()+60>=enemyTank_temp.getX()
                                        &&this.getX()+60<=enemyTank_temp.getX()+40
                                        &&this.getY()+40>=enemyTank_temp.getY()
                                        &&this.getY()+40<=enemyTank_temp.getY()+60){
                                    return true;//说明碰撞了
                                }
                                //敌人左和右的判断
                            }else if (enemyTank_temp.getDrection()==1||enemyTank_temp.getDrection()==3){
                                //判断当前坦克右上角坐标
                                if (this.getX()+60>=enemyTank_temp.getX()
                                        &&this.getX()+60<=enemyTank_temp.getX()+60
                                        &&this. getY()>=enemyTank_temp.getY()
                                        &&this.getY()<=enemyTank_temp.getY()+40){
                                    return true;//说明碰撞了
                                }
                                //获取当前坦克右下角坐标
                                if (this.getX()+60>=enemyTank_temp.getX()
                                        &&this.getX()+60<=enemyTank_temp.getX()+60
                                        &&this. getY()+40>=enemyTank_temp.getY()
                                        &&this.getY()+40<=enemyTank_temp.getY()+40){
                                    return true;//说明碰撞了
                                }
                            }
                        }
                    }
                }
                break;
            case 2:
                for (int i = 0; i < enemyTanks.size(); i++) {
                    //取出一辆坦克
                    EnemyTank enemyTank_temp = enemyTanks.get(i);
                    //先看坦克是否还存活
                    if (enemyTank_temp.isLive){
                        //判断比较的坦克是不是自己
                        if (enemyTank_temp!=this){
                            //判断敌人坦克是朝上还朝下
                            if (enemyTank_temp.getDrection()==0||enemyTank_temp.getDrection()==2){
                                //1.判断敌人x的范围 2.判断敌人y的范围
                                //判断当前坦克左下角坐标
                                if (this.getX()>=enemyTank_temp.getX()
                                        &&this.getX()<=enemyTank_temp.getX()+40
                                        &&this.getY()+60>=enemyTank_temp.getY()
                                        &&this.getY()+60<=enemyTank_temp.getY()+60){
                                    return true;//说明碰撞了
                                }
                                //获取当前坦克右下角坐标
                                if (this.getX()+40>=enemyTank_temp.getX()
                                        &&this.getX()+40<=enemyTank_temp.getX()+40
                                        &&this.getY()+60>=enemyTank_temp.getY()
                                        &&this.getY()+60<=enemyTank_temp.getY()+60){
                                    return true;//说明碰撞了
                                }
                                //敌人左和右的判断
                            }else if (enemyTank_temp.getDrection()==1||enemyTank_temp.getDrection()==3){
                                //判断当前坦克左下角坐标
                                if (this.getX()>=enemyTank_temp.getX()
                                        &&this.getX()<=enemyTank_temp.getX()+60
                                        &&this. getY()+60>=enemyTank_temp.getY()
                                        &&this.getY()+60<=enemyTank_temp.getY()+40){
                                    return true;//说明碰撞了
                                }
                                //判断当前坦克右下角坐标
                                if (this.getX()+40>=enemyTank_temp.getX()
                                        &&this.getX()+40<=enemyTank_temp.getX()+60
                                        &&this. getY()+60>=enemyTank_temp.getY()
                                        &&this.getY()+60<=enemyTank_temp.getY()+40){
                                    return true;//说明碰撞了
                                }
                            }
                        }
                    }
                }
                break;
            case 3:
                for (int i = 0; i < enemyTanks.size(); i++) {
                    //取出一辆坦克
                    EnemyTank enemyTank_temp = enemyTanks.get(i);
                    //先看坦克是否还存活
                    if (enemyTank_temp.isLive){
                        //判断比较的坦克是不是自己
                        if (enemyTank_temp!=this){
                            //判断敌人坦克是朝上还朝下
                            if (enemyTank_temp.getDrection()==0||enemyTank_temp.getDrection()==2){
                                //1.判断敌人x的范围 2.判断敌人y的范围
                                //判断当前坦克左上角坐标
                                if (this.getX()>=enemyTank_temp.getX()
                                        &&this.getX()<=enemyTank_temp.getX()+40
                                        &&this.getY()>=enemyTank_temp.getY()
                                        &&this.getY()<=enemyTank_temp.getY()+60){
                                    return true;//说明碰撞了
                                }
                                //判断当前坦克左下角坐标
                                if (this.getX()>=enemyTank_temp.getX()
                                        &&this.getX()<=enemyTank_temp.getX()+40
                                        &&this.getY()+40>=enemyTank_temp.getY()
                                        &&this.getY()+40<=enemyTank_temp.getY()+60){
                                    return true;//说明碰撞了
                                }
                                //敌人左和右的判断
                            }else if (enemyTank_temp.getDrection()==1||enemyTank_temp.getDrection()==3){
                                //判断当前坦克左上角坐标
                                if (this.getX()>=enemyTank_temp.getX()
                                        &&this.getX()<=enemyTank_temp.getX()+60
                                        &&this. getY()>=enemyTank_temp.getY()
                                        &&this.getY()<=enemyTank_temp.getY()+40){
                                    return true;//说明碰撞了
                                }
                                //获取当前坦克左下角坐标
                                if (this.getX()>=enemyTank_temp.getX()
                                        &&this.getX()<=enemyTank_temp.getX()+60
                                        &&this. getY()+40>=enemyTank_temp.getY()
                                        &&this.getY()+40<=enemyTank_temp.getY()+40){
                                    return true;//说明碰撞了
                                }
                            }
                        }
                    }
                }
                break;
        }
        //没有产生碰撞
        return false;
    }
}
